using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using System;
public abstract class abstuctSkill : MonoBehaviour
{
    /// <summary>
    /// 技能释放的初始位置
    /// </summary>
    public Transform initialTransform;
    public Vector3 initialPosition;
    /// <summary>
    /// 技能释放者
    /// </summary>
    public GameObject source;
    /// <summary>
    /// 技能基本伤害
    /// </summary>
    public float basicDamage;
    /// <summary>
    /// 前摇时间，单位ms
    /// </summary>
    public int frontDuration;
    /// <summary>
    /// 后摇时间
    /// </summary>
    public int backDuration;
    /// <summary>
    /// 冷却时间，单位ms
    /// </summary>
    public int cooldown = 0;
    /// <summary>
    /// 技能释放方向
    /// </summary>
    public Vector3 direction;
    /// <summary>
    /// 技能id
    /// </summary>
    public int id;
    /// <summary>
    /// 技能名
    /// </summary>
    public string skillName;
    /// <summary>
    /// 技能所带来的额外buff效果
    /// </summary>
    public List<GameObject> buffList;
    /// <summary>
    /// 技能是否可以被打断,默认为是，在受击时，技能会被强制结束
    /// </summary>
    public bool canInterrupted = true;
    /// <summary>
    /// 子类需要实现的方法,技能释放的时刻
    /// </summary>
    protected abstract void onExecute();
    /// <summary>
    /// 技能前摇开始
    /// </summary>
    protected virtual void onFrontDurationStart()
    {
        Debug.Log(source.gameObject.name + ":onFrontDurationStart");
        source.GetComponent<Animator>().SetBool("isSkilling", true);
        source.GetComponent<state>().isMovable = false;
        //如果当前技能不能被打断,就让角色进入硬直状态
        if (!canInterrupted)
        {
            source.GetComponent<state>().isStiff = true;
        }

    }
    /// <summary>
    /// 
    /// </summary>
    protected virtual void onFrontDurationEnd()
    {
        Debug.Log(source.gameObject.name + "onFrontDurationEnd");
        source.GetComponent<Animator>().SetBool("isSkilling", false);
        source.GetComponent<state>().isMovable = true;
        //退出硬直状态
        if (!canInterrupted)
        {
            source.GetComponent<state>().isStiff = false;
        }


    }
    /// <summary>
    /// 技能后摇开始
    /// </summary>
    protected virtual void onBackDurationStart()
    {
        Debug.Log(source.gameObject.name + "onBackDurationStart");
        source.GetComponent<state>().isMovable = false;

    }
    /// <summary>
    /// 技能后摇结束
    /// </summary>
    protected virtual void onBackDurationEnd()
    {
        Debug.Log(source.gameObject.name + "onBackDurationEnd");
        source.GetComponent<state>().isMovable = true;
        if (source.GetComponent<playerControl>()._controller.isGrounded)
        {
            source.GetComponent<state>().movementState = state.MovementState.IDLE;
        }
        else
        {
            source.GetComponent<state>().movementState = state.MovementState.FLOATING;

        }
    }
    /// <summary>
    /// 技能初始化
    /// </summary>
    protected virtual void init()
    {
        if (source == null)
        {
            source = FindObjectOfType<god>().gameObject;
        }
        this.gameObject.SetActive(false);
    }
    /// <summary>
    /// 技能的执行过程,使用多线程来达到前后摇效果
    /// </summary>
    private void execute()
    {
        Loom.QueueOnMainThread((a) =>
        {
            onFrontDurationStart();

        }, null);

        Thread.Sleep(frontDuration);
        Loom.QueueOnMainThread((a) =>
        {
            onFrontDurationEnd();
            onExecute();
            onBackDurationStart();

        }, null);


        Thread.Sleep(backDuration);

        Loom.QueueOnMainThread((a) =>
        {
            onBackDurationEnd();

        }, null);

    }
    /// <summary>
    /// 执行技能，请务必用这个函数来释放技能，除非特殊技能
    /// </summary>
    /// <param name="source"></param>
    public virtual void executeSkill(GameObject source)
    {
        this.source = source;
        source.GetComponent<state>().movementState = state.MovementState.PLAY_SKILL;
        new Thread(() => { execute(); }).Start();
    }
    /// <summary>
    /// 将设置的所有buff效果作用于gameobject
    /// </summary>
    public virtual void addBuffsToPlayer(GameObject target)
    {
        for (int i = 0; i < buffList.Count; i++)
        {
            target.GetComponent<playerControl>().addBuff(buffList[i]);
        }
    }
    /// <summary>
    /// 打断技能
    /// </summary>
    public virtual void interruptSkill()
    {
        //只有可以被打断的技能才会被打断
        if (this.canInterrupted) { Destroy(this.gameObject); }

    }
}
